home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / QuickDraw3D 1.6 SDK / Mac SampleCode Previous / Picking Samples / MarkerPick / MarkerPickShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  8.4 KB  |  329 lines  |  [TEXT/MPCC]

  1. // MarkerPickShell.c - QuickDraw 3d routines
  2. //
  3. // This is MarkerPick, an adaptation of box, the QuickDraw 3D starter program,
  4. // written by Nick Thompson.
  5. //
  6. // Julian Gómez - May 31, 1995
  7. // 
  8. // ©1994-95 Apple computer Inc., All Rights Reserved
  9. //
  10.  
  11. // system headers
  12. #include <Dialogs.h>
  13. #include <DiskInit.h>
  14. #include <Fonts.h>
  15. #include <Menus.h>
  16. #include <PictUtils.h>
  17. #include <QDOffScreen.h>
  18. #include <QuickDraw.h>
  19. #include <SegLoad.h>
  20. #include <StandardFile.h>
  21. #include <TextEdit.h>
  22.  
  23. // for QuickDraw 3D
  24. #include "QD3D.h"
  25. #include "QD3DMath.h"
  26. #include "QD3DDrawContext.h"
  27. #include "QD3DShader.h"
  28. #include "QD3DTransform.h"
  29. #include "QD3DGroup.h"
  30. #include "QD3DPick.h"
  31.  
  32.  
  33. #include "MarkerPickShell.h"
  34. #include "MarkerPickSupport.h"
  35.  
  36. //-------------------------------------------------------------------------------------------
  37.  
  38. struct _documentRecord {
  39.     TQ3ViewObject    fView ;                    // the view for the scene
  40.     TQ3GroupObject    fModel ;                // object in the scene being modelled
  41.     TQ3StyleObject    fInterpolation ;        // interpolation style used when rendering
  42.     TQ3StyleObject    fBackFacing ;            // whether to draw shapes that face away from the camera
  43.     TQ3StyleObject    fFillStyle ;            // whether drawn as solid filled object or decomposed to components
  44.     TQ3Matrix4x4        fRotation;                // the transform for the model
  45. };
  46.  
  47. typedef struct _documentRecord DocumentRec, *DocumentPtr, **DocumentHdl ;
  48.  
  49.  
  50. //-------------------------------------------------------------------------------------------
  51. // function prototypes
  52.  
  53. static void         InitToolbox( void ) ;
  54. static void         MainEventLoop( void ) ;
  55. static void         HandleKeyPress(EventRecord *event) ;
  56. static void         HandleOSEvent(EventRecord *event) ;
  57. void InitDocumentData( DocumentPtr theDocument ) ;
  58. TQ3Status DocumentDraw3DData( DocumentPtr theDocument ) ;
  59. void DisposeDocumentData( DocumentPtr theDocument) ;
  60.  
  61. void DoPick(Point, DocumentPtr);
  62.  
  63.  
  64. //-------------------------------------------------------------------------------------------
  65. //
  66.  
  67. Boolean         gQuitFlag         = false ;
  68. WindowPtr        gMainWindow        = nil ;
  69. DocumentRec        gDocument ;
  70.  
  71. //-------------------------------------------------------------------------------------------
  72. // main()
  73. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  74. // and enter the main event loop.  On exit from the main event loop, we want to call
  75. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  76.  
  77. void main(void)
  78. {
  79.     TQ3Status    myStatus;
  80.     Rect        rBounds = { 50, 50, 450, 450 } ;
  81.     Str255        title = "\pMarker Pick" ;
  82.  
  83.     InitToolbox() ;
  84.     
  85.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  86.     myStatus = Q3Initialize();
  87.  
  88.     if ( myStatus == kQ3Failure )
  89.         DebugStr("\pErInitialize returned failure.");            
  90.  
  91.     // set up our globals
  92.     gQuitFlag = false ;
  93.     gMainWindow = NewCWindow(nil,&rBounds,title,true,noGrowDocProc,(WindowPtr)-1,true,0) ;
  94.  
  95.     InitDocumentData( &gDocument ) ;
  96.     
  97.     MainEventLoop();
  98.     
  99.     DisposeDocumentData( &gDocument ) ;
  100.     
  101.     //    Close our connection to the QuickDraw 3D library
  102.     myStatus = Q3Exit();
  103.     if ( myStatus == kQ3Failure )
  104.         DebugStr("\pErExit returned failure.");
  105.     
  106. }
  107.  
  108. //-------------------------------------------------------------------------------------------
  109. //
  110.  
  111. void InitDocumentData( DocumentPtr theDocument ) 
  112. {
  113.     // sets up the 3d data for the scene
  114.     // Create view for QuickDraw 3D.
  115.     theDocument->fView = MyNewView( (WindowPtr)gMainWindow ) ;
  116.  
  117.     // the main display group:
  118.     theDocument->fModel = MyNewModel() ;
  119.  
  120.     // the drawing styles:
  121.     theDocument->fInterpolation = Q3InterpolationStyle_New(kQ3InterpolationStyleNone) ;
  122.     theDocument->fBackFacing = Q3BackfacingStyle_New(kQ3BackfacingStyleBoth ) ;
  123.     theDocument->fFillStyle = Q3FillStyle_New(kQ3FillStyleFilled ) ;
  124.  
  125.     // set the rotation matrix the identity matrix
  126.     Q3Matrix4x4_SetIdentity(&theDocument->fRotation);        
  127. }
  128.  
  129. void DisposeDocumentData( DocumentPtr theDocument)
  130. {
  131.     Q3Object_Dispose(theDocument->fView) ;                // the view for the scene
  132.     Q3Object_Dispose(theDocument->fModel) ;                // object in the scene being modelled
  133.     Q3Object_Dispose(theDocument->fInterpolation) ;        // interpolation style used when rendering
  134.     Q3Object_Dispose(theDocument->fBackFacing) ;        // whether to draw shapes that face away from the camera
  135.     Q3Object_Dispose(theDocument->fFillStyle) ;            // whether drawn as solid filled object or decomposed to components
  136.  
  137. }
  138. //-----------------------------------------------------------------------------
  139. // 
  140.  
  141. TQ3Status DocumentDraw3DData( DocumentPtr theDocument )
  142. {    
  143.     Q3View_StartRendering(theDocument->fView );
  144.     do {
  145.         Q3Style_Submit( theDocument->fInterpolation, theDocument->fView );
  146.         Q3Style_Submit( theDocument->fBackFacing, theDocument->fView );
  147.         Q3Style_Submit( theDocument->fFillStyle, theDocument->fView );
  148.         Q3MatrixTransform_Submit( &theDocument->fRotation, theDocument->fView );
  149.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  150.     } while (Q3View_EndRendering(theDocument->fView) == kQ3ViewStatusRetraverse );
  151.     return kQ3Success ;
  152. }
  153.  
  154.  
  155. //----------------------------------------------------------------------------------
  156.  
  157. //-------------------------------------------------------------------------------------------
  158. //
  159.  
  160. short HiWrd(long aLong)
  161. {
  162.     return    (((aLong) >> 16) & 0xFFFF) ;
  163. }
  164.  
  165. //-------------------------------------------------------------------------------------------
  166. //
  167.  
  168. short LoWrd(long aLong)
  169. {
  170.     return    ((aLong) & 0xFFFF) ;
  171.  
  172. }
  173.  
  174. //-------------------------------------------------------------------------------------------
  175. //
  176.  
  177. void InitToolbox()
  178. {
  179.     Handle        menuBar = nil;
  180.  
  181.     MaxApplZone() ;
  182.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  183.     
  184.     InitGraf( &qd.thePort );
  185.     InitFonts();
  186.     InitWindows();
  187.     InitCursor();
  188.  
  189.     FlushEvents( everyEvent, 0 ) ;
  190.     // initialize application globals
  191.     
  192.     gQuitFlag = false;
  193.     
  194. }
  195.  
  196.  
  197. //-------------------------------------------------------------------------------------------
  198. //
  199. void MainEventLoop()
  200. {
  201.     EventRecord     event;
  202.     WindowPtr       window;
  203.     short           thePart;
  204.     Rect            screenRect, updateRect;
  205.     Point            aPoint = {100, 100};
  206.     Point            clickPoint;
  207.     CGrafPtr        savedPort ;
  208.     
  209.  
  210.     while( !gQuitFlag )
  211.     {
  212.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  213.         {
  214.  
  215.             switch (event.what) {
  216.                 case mouseDown:
  217.                 
  218.                     thePart = FindWindow( event.where, &window );
  219.                     
  220.                     switch( thePart ) {
  221.                         case inMenuBar: 
  222.                             break;
  223.                         
  224.                         case inDrag:
  225.                     
  226.                             screenRect = (**GetGrayRgn()).rgnBBox;
  227.                             DragWindow( window, event.where, &screenRect );
  228.                             break ;
  229.                     
  230.                         case inContent:
  231.                     
  232.                             if (window != FrontWindow())
  233.                                 SelectWindow( window );
  234.                             else {
  235.                                 GetPort((GrafPtr*)&savedPort);
  236.                                 SetPort(window);
  237.                                 clickPoint = event.where;
  238.                                 GlobalToLocal(&clickPoint);
  239.                                 SetPort((GrafPtr)savedPort);
  240.                                 DoPick(clickPoint, &gDocument);
  241.                             }
  242.                             break ;
  243.                     
  244.                         case inGoAway:
  245.                             if (TrackGoAway( window, event.where )) {
  246.                                 DisposeWindow ( window );
  247.                                 gQuitFlag = true;
  248.  
  249.                             }
  250.                             break ;
  251.                             
  252.                         default:
  253.                             break ;
  254.                     }
  255.                     break ;
  256.                             
  257.                         
  258.                 case updateEvt:
  259.                 
  260.                     window = (WindowPtr)event.message;
  261.                     updateRect = (**(window->visRgn)).rgnBBox;
  262.                     SetPort( window ) ;
  263.                     BeginUpdate( window );
  264.                     DocumentDraw3DData( &gDocument ) ;
  265.                     EndUpdate( window );
  266.                     break ;
  267.                     
  268.                 case keyDown:
  269.                 case autoKey:
  270.                     HandleKeyPress(&event);
  271.                     break;
  272.                     
  273.                 case diskEvt:
  274.                     if ( HiWrd(event.message) != noErr ) 
  275.                         (void) DIBadMount(aPoint, event.message);
  276.                     break;
  277.                     
  278.                 case osEvt:
  279.                 case activateEvt:
  280.                     break;
  281.  
  282.  
  283.             }
  284.         }
  285.     }
  286. }
  287.  
  288.  
  289. //-------------------------------------------------------------------------------------------
  290. //
  291. void HandleKeyPress(EventRecord *event)
  292. {}
  293.  
  294. //-------------------------------------------------------------------------------------------
  295. //
  296.  
  297.  
  298.  
  299. void DoPick(Point where, DocumentPtr theDocument)
  300.     {
  301.     TQ3WindowPointPickData    data;
  302.     unsigned long            i;
  303.     unsigned long            numHits;
  304.     TQ3PickObject            pick;
  305.     
  306.     data.data.sort = kQ3PickSortNone;
  307.     data.data.mask = 0;
  308.     data.data.numHitsToReturn = kQ3ReturnAllHits;
  309.     
  310.     data.point.x = where.h;
  311.     data.point.y = where.v;
  312.     data.vertexTolerance = 1;
  313.     data.edgeTolerance = 1;
  314.     
  315.     if ((pick = Q3WindowPointPick_New(&data)) == NULL)
  316.         return;
  317.         
  318.     Q3View_StartPicking(theDocument->fView, pick);
  319.     do {
  320.         Q3DisplayGroup_Submit( theDocument->fModel, theDocument->fView );
  321.     } while (Q3View_EndPicking(theDocument->fView) == kQ3ViewStatusRetraverse );
  322.     
  323.     if (Q3Pick_GetNumHits(pick, &numHits) == kQ3Success)
  324.         for (i=0; i<numHits; i++)
  325.             SysBeep(5);
  326.             
  327.     Q3Object_Dispose(pick);
  328.     }
  329.